package com.cola.action;

import com.cola.entity.export.ExportMep;
import com.cola.service.CommonService;
import com.cola.service.MepService;
import com.cola.utils.Struts2Utils;
import org.apache.commons.lang3.StringUtils;
import org.wuwz.poi.ExcelKit;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 经销商Action
 * @author Max
 * @since 2017/9/6
 */
public class MepAction extends BaseAction{
    private Integer offset;
    private Integer limit;
    private String mep_mobile;
    private String area;
    private Integer status=-2;
    private String mep_name;
    private Integer is_bind=-2;
    private Integer mep_id;
    private String mep_contact_name;
    private String address;
    //网络文件URl
    private String fileUrl;
    //文件名
    private String filename;

    private String mep_no;

    private String city;

    /**
     * 跳转到经销商列表页
     * @author Max
     * @since 2017/9/6
     */
    public String toMepList(){
        setRequestAttribute("cityList", CommonService.getInstance().getCityList());
        return SUCCESS;
    }

    /**
     * 获取经销商列表
     * @author Max
     * @since 2017/9/6
     */
    public void getMepList(){
        Map<String,Object> resultMap = new HashMap<String,Object>();

        resultMap.put("rows", MepService.getInstance().getMepList(offset,limit,getMepSearchFilter()));

        resultMap.put("total",MepService.getInstance().getMepCount(getMepSearchFilter()));

        Struts2Utils.renderJson(resultMap);
    }

    /**
     * 根据MepId获取Mep信息
     * @author Max
     * @since 2017/9/6
     */
    public void getMepInfoById(){
        Map<String,Object> resultMap = new HashMap<String,Object>();

        Map<String,Object> mepInfoMap = MepService.getInstance().getMepInfoById(mep_id);

        if(mepInfoMap==null){
            resultMap.put("result","error");
        }else{
            resultMap.put("result","success");
            resultMap.put("mepInfo",mepInfoMap);
        }

        Struts2Utils.renderJson(resultMap);
    }

    /**
     * 保存MEP信息
     * @author Max
     * @since 2017/9/6
     */
    public void saveMepInfo(){
        Map<String,Object> resultMap = new HashMap<String,Object>();

        if(MepService.getInstance().saveMepInfo(mep_id,mep_name,mep_mobile,mep_contact_name,address,city)){
            resultMap.put("result","success");
        }else{
            resultMap.put("result","error");
        }

        Struts2Utils.renderJson(resultMap);
    }

    /**
     * 获取搜索经销商搜索条件
     * @author Max
     * @since 2017/9/6
     */
    private String getMepSearchFilter(){
        StringBuilder filter = new StringBuilder("");
        if(StringUtils.isNotEmpty(mep_mobile)){
            filter.append(" and mep_mobile = '").append(mep_mobile).append("'");
        }

        if(StringUtils.isNotEmpty(area)){
            filter.append(" and area = '").append(area).append("'");
        }

        if(status>-2){
            switch (status){
                case 1:filter.append(" and a.`status` = '").append(1).append("'").append(" and is_bind = '").append(1).append("'");break;
                case 0: filter.append(" and a.`status` = '").append(1).append("'").append(" and is_bind = '").append(0).append("'");break;
                case -1: filter.append(" and a.`status` = ").append(-1).append("");break;
            }
        }

//        if(is_bind>-2){
//            filter.append(" and is_bind = '").append(is_bind).append("'");
//        }

        if(StringUtils.isNotEmpty(mep_no)){
            filter.append(" and mep_no = '").append(mep_no).append("'");
        }

        if(StringUtils.isNotEmpty(city)){
            filter.append(" and city = '").append(city).append("'");
        }

        System.out.println(filter.toString());
        return filter.toString();
    }

    /**
     * 导入经销商
     * @author Max
     * @since 2017/9/6
     */
    public void importMepInfo(){
        Map<String,Integer> resultMap = new HashMap<String,Integer>();
        resultMap = MepService.getInstance().importMepInfo(fileUrl,getUserName(),filename,getUserId());
        Struts2Utils.renderJson(resultMap);
    }

    /**
     * 删除经销商
     * @author Max
     * @since 2017/9/6
     */
    public void deleteMep(){
        Map<String,String> resultMap = new HashMap<String,String>();
        if(MepService.getInstance().deleteMep(mep_id)){
            resultMap.put("result","success");
        }else{
            resultMap.put("result","error");
        }
        Struts2Utils.renderJson(resultMap);
    }

    /**
     * 禁封经销商
     * @author Max
     * @since 2017/9/6
     */
    public void stopMep(){
        Map<String,String> resultMap = new HashMap<String,String>();
        if(MepService.getInstance().stopMep(mep_id)){
            resultMap.put("result","success");
        }else{
            resultMap.put("result","error");
        }
        Struts2Utils.renderJson(resultMap);
    }

    /**
     * 禁封经销商
     * @author Max
     * @since 2017/9/6
     */
    public void startMep(){
        Map<String,String> resultMap = new HashMap<String,String>();
        if(MepService.getInstance().startMep(mep_id)){
            resultMap.put("result","success");
        }else{
            resultMap.put("result","error");
        }
        Struts2Utils.renderJson(resultMap);
    }


    /**
     * 跳转到导入记录页面
     * @author Max
     * @since 2017/9/6
     */
    public String toImportRecord(){
        return SUCCESS;
    }

    /**
     * 获取经销商信息导入记录
     * @author Max
     * @since 2017/9/6
     */
    public void getMepImportRecords(){
        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put("rows", MepService.getInstance().getMepImportRecord(offset,limit));

        resultMap.put("total",MepService.getInstance().getMepImportRecordCount());

        Struts2Utils.renderJson(resultMap);
    }

    /**
     * 导出经销商列表
     * @author Max
     * @since 2017/9/6
     */
    public void exportMepInfoList() throws Exception{
        List<ExportMep> list = MepService.getInstance().getExportMepList();
        ExcelKit.$Export(ExportMep.class,getResponse()).toExcel(list,
                "MEP信息表");
    }


    public Integer getOffset() {
        return offset;
    }

    public void setOffset(Integer offset) {
        this.offset = offset;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public String getMep_mobile() {
        return mep_mobile;
    }

    public void setMep_mobile(String mep_mobile) {
        this.mep_mobile = mep_mobile;
    }

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMep_name() {
        return mep_name;
    }

    public void setMep_name(String mep_name) {
        this.mep_name = mep_name;
    }

    public Integer getIs_bind() {
        return is_bind;
    }

    public void setIs_bind(Integer is_bind) {
        this.is_bind = is_bind;
    }

    public Integer getMep_id() {
        return mep_id;
    }

    public void setMep_id(Integer mep_id) {
        this.mep_id = mep_id;
    }

    public String getMep_contact_name() {
        return mep_contact_name;
    }

    public void setMep_contact_name(String mep_contact_name) {
        this.mep_contact_name = mep_contact_name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getFileUrl() {
        return fileUrl;
    }

    public void setFileUrl(String fileUrl) {
        this.fileUrl = fileUrl;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String getMep_no() {
        return mep_no;
    }

    public void setMep_no(String mep_no) {
        this.mep_no = mep_no;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}
